#!/bin/sh

echo "###############################################################"
echo "### 新统一支付平台(NUPP)更新应用程序验证脚本 "
echo "###############################################################"

APPPATH=$(cd `dirname $0`;pwd)
UPPACKAGE_DIR="$HOME/package_update"
TEMP_DIR=".tmpapp"

update_one_app(){	
  app_name="nupp-$1"
  # 创建临时目录
  if [ ! -d $TEMP_DIR ]; then
    mkdir $TEMP_DIR
  fi
  cd $TEMP_DIR
  # 获取最新的程序包
  package_name=$(ls -lrt ${UPPACKAGE_DIR} | grep "${app_name}" | tail -n 1 | awk 'NF>8{print $NF}')
  if [ -z "$package_name" ]; then
    echo "应用程序[${app_name}]没有待更新的程序包，请核实..."
    return -1
  fi

  tar -zxvf $UPPACKAGE_DIR/$package_name
  if [ $? != 0 ]; then
    echo "解压包[$package_name]失败，请核实是否是以.tar.gz结尾的包"
    return -1
  fi

  # 先备份待更新的应用程序
  if [ ! -d $UPPACKAGE_DIR/bak ]; then
    mkdir -p $UPPACKAGE_DIR/bak
  fi
  if [ $1 == "mp" ]; then
    cd $APPPATH/nginx
    tar -cvf dist.tar dist 
    mv dist.tar $UPPACKAGE_DIR/bak
    rm -rf dist
  else
    cd $APPPATH/${app_name}
    tar -cvf ${app_name}.tar --ignore-failed-read lib *.jar
    mv ${app_name}.tar $UPPACKAGE_DIR/bak
    rm -rf *.jar lib
  fi
  

  # 更新包至应用程序部署目录
  if [ $1 == "mp" ]; then
    cd $APPPATH/nginx

    tar -xvf $APPPATH/$TEMP_DIR/dist.tar
    if [ $? == 0 ]; then
      echo "应用程序[${app_name}]已成功更新，请重启nginx后生效"
    else
      echo "应用程序[${app_name}]更新失败，请检查..."
    fi
  else
    cd $APPPATH/$TEMP_DIR
    cp -r ${app_name}*/lib $APPPATH/${app_name}
    cp ${app_name}*/*.jar $APPPATH/${app_name}
    if [ $? == 0 ]; then
      echo "应用程序[${app_name}]已成功更新，请重启应用后生效"
    else 
      echo "应用程序[${app_name}]更新失败，请检查..."
    fi
  fi
}

rm_temp_dir(){
  cd $APPPATH
  if [ -d $TEMP_DIR ]; then
    rm -rf $TEMP_DIR
  fi
}

usage() {
  echo "用法: $0 [参数]"
  echo "参数:"
  echo "    all           更新所有的应用程序包"
  echo "    mp            更新管理端程序包"
  echo "    settings      更新服务nupp-settings    程序包"
  echo "    notice        更新服务nupp-notice      程序包"
  echo "    security      更新服务nupp-security    程序包"
  echo "    gateway       更新服务nupp-gateway     程序包"
  echo "    routing       更新服务nupp-routing     程序包"
  echo "    channel       更新服务nupp-channel     程序包"
  echo "    business      更新服务nupp-business    程序包"
  echo "    checking      更新服务nupp-checking    程序包"
  echo "    workflow      更新服务nupp-workflow    程序包"
  echo "    scheduler     更新服务nupp-scheduler   程序包"
  echo "    analysis      更新服务nupp-analysis    程序包"
  echo "    position      更新服务nupp-position    程序包"
  echo "    api-gateway   更新服务nupp-api-gateway 程序包"

}

#### MAIN ####
if [ $# == 0 ]; then 
  usage
elif [ $# == 1 ]; then
  case $1 in 
    "channel"|"business"|"checking"|"workflow"|"scheduler"|"analysis"|"notice"|"position"|"settings"|"routing"|"gateway"|"security"|"mp"|"api-gateway")
      update_one_app $1
      rm_temp_dir
      ;;
    "all")
      # 更新所有程序
      update_one_app "settings"
      update_one_app "notice"
      update_one_app "security"
      update_one_app "gateway"
      update_one_app "routing"
      update_one_app "channel"
      update_one_app "business"
      update_one_app "checking"
      update_one_app "workflow"
      update_one_app "scheduler"
      update_one_app "analysis"
      update_one_app "position"
      update_one_app "mp"
      update_one_app "api-gateway"
      rm_temp_dir
      ;;	  
    *)
      echo "invalid parameter [$1]..."
      ;;	  
  esac
fi



